﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NewBranch : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (isLogin())
        {
            if (LoginedStaff.tblRole.Role != "Admin")
                Response.Redirect("~/Admin/Index.aspx");
        }
        else
        {
            Response.Redirect("~/Admin/Stafflogin.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        branchLabel.Visible = false;
        addressLabel.Visible = false;

        if (!string.IsNullOrEmpty(branchName.Text) && !string.IsNullOrEmpty(branchAddress.Text))
        {
            MailDistributionSystemDBDataContext db = new MailDistributionSystemDBDataContext();
            if (db.tblBranches.Where(c => c.Name == branchName.Text.Trim()).Count() == 0)
            {
                tblBranch branch = new tblBranch();
                branch.Name = branchName.Text.Trim();
                branch.Address = branchAddress.Text.Trim();
                branch.CountryID = Convert.ToInt16(countryList.SelectedValue);
                db.tblBranches.InsertOnSubmit(branch);
                db.SubmitChanges();
                Response.Redirect("~/Admin/Branch/ViewBranch.aspx");
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Branch Already Exist!')", true);
            }
        }
        else
        {
            if (string.IsNullOrEmpty(branchName.Text))
            {
                branchLabel.Visible = true;
            }

            if (string.IsNullOrEmpty(branchAddress.Text))
            {
                addressLabel.Visible = true;
            }
        }
    }
}